/*
 * 文 件 名 : HomeActivity.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-4
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fire8.dolphin.db.QuestionDAO;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.ui.diary.QuestionAddActivity;
import com.fire8.dolphin.utils.StringUtil;

/**
 * 我的提问
 * 显示全部我自己提出的问题
 *
 * @author  lixg
 * @version [版本号,2012-8-4]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class MsgActivity extends Activity implements OnItemClickListener
{
    private final String TAG="dolphin";
    private Button btnRight;
    private Button btnLeft;
    private ListView listView;
    
    private QuestionDAO dao;
    private List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    
    private MyAdapter myAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_list);
        TextView tvTitle = (TextView)findViewById(R.id.tv_bar_title);
        //tvTitle.setText("我的提问");
        new MyAdapter(this);
        btnRight = (Button)findViewById(R.id.title_bt_right);
        btnRight.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MsgActivity.this,QuestionAddActivity.class);
                startActivity(intent);
            }
        });
        btnLeft = (Button)findViewById(R.id.title_bt_left);
        btnLeft.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MsgActivity.this,QuestionAddActivity.class);
                startActivity(intent);
            }
        });
//        myAdapter = new MyAdapter(this);
//        dao = new QuestionDAO(this);
//        listView = (ListView)findViewById(R.id.question_list);
//        listView.setOnItemClickListener(this);
//        listView.setAdapter(myAdapter);
//        findQuestionList();
    }



    /**
     * 从本地库中查询问题列表
     * <功能详细描述> [参数说明]
     *
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    private void findQuestionList()
    {
        DolphinApplication app = (DolphinApplication)getApplication();
        Question question = new Question();
        question.setUserId(app.getUserId()==""?"0":app.getUserId());
        //list = dao.findList(question,Question.QUERY_UP,app.PAGE_SIZE,"1");
        myAdapter.notifyDataSetChanged();
    }
    
    
    
    @Override
    protected void onStart()
    {
        super.onStart();
        //findQuestionList();
    }



    public final class ViewHolder
    {
        
        public TextView tvContext;
        public TextView tvAnswerNum;
        public TextView tvDate;
        public TextView tvUserName;
        public ImageView imgHead;
    }
    public class MyAdapter extends BaseAdapter
    {
        private LayoutInflater mInflater; 
        public MyAdapter(Context context)
        {
            this.mInflater = LayoutInflater.from(context); 
        }
        
        public int getCount()
        {
            return list.size();
        }

        
        public Object getItem(int position)
        {
            return null;
        }

        
        public long getItemId(int position)
        {
            return 0;
        }

        
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder = null; 
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.msg_item, null);
                holder.tvContext = (TextView)convertView.findViewById(R.id.tv_context);
                holder.tvAnswerNum = (TextView)convertView.findViewById(R.id.tv_answer_num);
                holder.tvDate = (TextView)convertView.findViewById(R.id.tv_date);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }
            HashMap<String, String> map= list.get(position);
            String voice = map.get(Question.C_VOICE);
            if(!StringUtil.isEmpty(voice))
            {
                //有语音，显示语音图标
            }
            holder.tvContext.setText(map.get(Question.C_CONTEXT)); 
            holder.tvAnswerNum.setText(map.get(Question.C_ANSWER_NUM));
            holder.tvDate.setText(map.get(Question.C_UPDATE_DATE));
            //holder.tvContext.setTag(map.get("t_id"));
            //holder.tvContext.setTag(2, map.get("t_remote_id"));
            //Log.d("haoyuntu","start date :"+(String)mData.get(position).get("start_date"));
            
            return convertView;
        }
        
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id)
    {
        HashMap<String, String> map= list.get(position);
        String locId = map.get(Question.C_Q_ID);
        String remoteId = map.get(Question.C_REMOTE_ID);
        Log.i(TAG,"发送的locId: "+locId);
        Intent intent = new Intent(this,AnswerActivity.class);
        intent.putExtra("from", "self");
        intent.putExtra("locId", locId);
        intent.putExtra("remoteId", remoteId);
        startActivity(intent);
    }
}
